package com.oa.dao.impl;

import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.enums.FlowFileType;
import com.oa.common.enums.FlowStatus;
import com.oa.common.enums.PersonType;
import com.oa.common.enums.apps.AppsSearchStatus;
import com.oa.common.eums.EntityStatus;
import com.oa.common.interfaces.PreFilter;
import com.oa.dao.AppsDao;
import com.oa.dao.BaseDaoImpl;
import com.oa.dao.PreFilters;
import com.oa.entities.flows.Apps;
import javafx.scene.shape.Circle;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import javax.security.auth.login.CredentialException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class AppsDaoImpl extends BaseDaoImpl<Long,Apps> implements AppsDao {

    @Override
    protected void loadClassType() {
        setType(Apps.class);
    }

    @Override
    public Page<Apps> findPage(Pageable pageable, String key, Integer type,Long userId) {
        StringBuilder stringBuilder= new StringBuilder();
        stringBuilder.append("select apps from Apps apps join apps.personnel personnel ");
        if (type==AppsSearchStatus.FLOW_OVER.getCode()){
            stringBuilder.append(" join personnel.flow flow ");
        }
        stringBuilder.append("where apps.status=:status");
        stringBuilder.append(" and personnel.userId=:userId");
        if (!StringUtils.isEmpty(key)){
            stringBuilder.append(" and (apps.name like :key or apps.title like :key)");
        }
        if (type==AppsSearchStatus.TIMEOUT.getCode()){
            stringBuilder.append(" and apps.issueTime<'"+(System.currentTimeMillis()-3600000*48)+"'");
            stringBuilder.append(" and apps.appsType=:code");
        }else if (type==AppsSearchStatus.OVERCOME.getCode()){
            stringBuilder.append(" and apps.title like :word");
        } else if (type==AppsSearchStatus.OVER.getCode()){
            stringBuilder.append(" and apps.appsType!=:code and apps.appsType!=:turn");
        } else if (type==AppsSearchStatus.FLOW_OVER.getCode()){
            stringBuilder.append(" and flow.flowStatus= :code");
        }else if (type==AppsSearchStatus.ALL.getCode()){
            stringBuilder.append(" and (apps.appsType=:code or apps.appsType=:number)");
        }
        Query query= getSession().createQuery(stringBuilder.toString()).setParameter("status",EntityStatus.ENTITY_EABLE.getCode())
                .setParameter("userId",userId)
                .setFirstResult((pageable.getPageNumber()-1)*pageable.getPageSize())
                .setMaxResults(pageable.getPageSize());
        if(!StringUtils.isEmpty(key)){
            key="%"+key+"%";
            query.setParameter("key",key);
        }
        if (type==AppsSearchStatus.TIMEOUT.getCode()){
            query.setParameter("code",PersonType.UNAUDITED.getCode());
        }else if (type==AppsSearchStatus.OVERCOME.getCode()){
            query.setParameter("word","%脱贫攻坚%");
        }else if (type==AppsSearchStatus.OVER.getCode()){
            query.setParameter("code",PersonType.UNAUDITED.getCode());
            query.setParameter("turn",PersonType.WAIT_AUDIT.getCode());
        }else if (type== AppsSearchStatus.FLOW_OVER.getCode()){
            query.setParameter("code",FlowStatus.END.getCode());
        }else if (type==AppsSearchStatus.ALL.getCode()){
            query.setParameter("code",PersonType.UNAUDITED.getCode());
            query.setParameter("number",PersonType.RETURN.getCode());
        }
        List<Apps> content= query.list();
        Criteria criteria= getCriteria();
        criteria.createAlias("personnel","personnel");
        criteria.add(Restrictions.eq(Apps.PROP_STATUS,EntityStatus.ENTITY_EABLE.getCode()));
        criteria.add(Restrictions.eq("personnel.userId",userId));
        if (type==AppsSearchStatus.TIMEOUT.getCode()){
            criteria.add(Restrictions.le(Apps.APPS_SENDTIME,System.currentTimeMillis()-3600000*48));
            criteria.add(Restrictions.eq(Apps.APPS_APPS_TYPE,PersonType.UNAUDITED.getCode()));
        }else if (type==AppsSearchStatus.OVERCOME.getCode()){
            criteria.add(Restrictions.like(Apps.APPS_TITLE,"%脱贫攻坚%"));
        }else if(type==AppsSearchStatus.OVER.getCode()){
            criteria.add(Restrictions.ne(Apps.APPS_APPS_TYPE,PersonType.UNAUDITED.getCode()));
            criteria.add(Restrictions.ne(Apps.APPS_APPS_TYPE,PersonType.WAIT_AUDIT.getCode()));
        }else if (type==AppsSearchStatus.FLOW_OVER.getCode()){
            criteria.createAlias("personnel.flow","flow");
            criteria.add(Restrictions.eq("flow.flowStatus",FlowStatus.END.getCode()));
        }else if (type==AppsSearchStatus.ALL.getCode()){
            criteria.add(Restrictions.or(Restrictions.eq(Apps.APPS_APPS_TYPE,PersonType.UNAUDITED.getCode()),
                    Restrictions.eq(Apps.APPS_APPS_TYPE,PersonType.RETURN.getCode())));
        }
        if (!StringUtils.isEmpty(key)){
            criteria.add(Restrictions.or(Restrictions.like(Apps.APPS_NAME,key),
                    Restrictions.like(Apps.APPS_TITLE,key)));
        }
        long count= (long) criteria.setProjection(Projections.rowCount()).uniqueResult();
        return new Page<>(pageable,content,new Long(count).intValue());
    }
}
